package br.com.iswe.gwt.ui.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.com.iswe.gwt.ui.client.img.CommonImageBundle;
import br.com.iswe.gwt.util.client.Listener;

import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;

public class ISWEImageFactory {

	private static final String PRESSED_BUTTON = "pressedButton";
	private static CommonImageBundle commonImageBundle = null;

	//Metodo q informa qual a fonte das imagens q sera utilizado
	public static void initialize(CommonImageBundle commonImageBundleParam){
		ISWEImageFactory.commonImageBundle = commonImageBundleParam;
	}
	
	private static void isInitialized() {
		if(commonImageBundle == null){
			throw new RuntimeException("ISWEImageFactory was not initialized! Call ISWEImageFactory.initialize(CommonImageBundle);");
		}
	}
	
	public static CommonImageBundle getCommonImageBundle(){
		return commonImageBundle;
	}
	
	public static ButtonBase inicioButton() {
		return createButton(commonImageBundle.botaoInicio());
	}
	
	public static ButtonBase anteriorButton() {
		return createButton(commonImageBundle.botaoAnterior());
	}
	
	public static ButtonBase proximoButton() {
		return createButton(commonImageBundle.botaoProximo());
	}
	
	public static ButtonBase ultimoButton() {
		return createButton(commonImageBundle.botaoUltimo());
	}
	
	public static ButtonBase createButton(AbstractImagePrototype imagePrototype){
		isInitialized();
		
		Image imgUp = imagePrototype.createImage();
		Image imgDown = imagePrototype.createImage();
		imgDown.addStyleName(PRESSED_BUTTON);
		PushButton button = new PushButtonISWEImpl(imgUp, imgDown);
		return button;
	}
		
	public static ButtonBase lupaButton() {
		return createButton(commonImageBundle.lupa());
	}

	public static ButtonBase createButtonFechar() {
		ButtonBase button = new ButtonISWEImpl("Fechar");//TODO:Cria uma imagem para o botao fechar.
		return button;
	}

	public static ButtonBase createButtonVerificar() {
		ButtonBase button = new ButtonISWEImpl("Verificar");//TODO:Cria uma imagem para o botao Verificar.
		return button;
	}

	public static ButtonBase createButtonAdicionar() {
		ButtonBase button = new ButtonISWEImpl("Adicionar");//TODO:Cria uma imagem para o botao Adicionar.
		return button;
	}

	public static ButtonBase createButtonLogin() {
		ButtonBase button = new ButtonISWEImpl("Efetuar Login");//TODO:Cria uma imagem para o botao Efetuar Login.
		return button;
	}

	public static ButtonBase createButtonProximoPasso() {
		ButtonBase button = new ButtonISWEImpl("Proximo Passo");
		return button;
	}
	
	public static ButtonBase createButton(String text) {
		ButtonISWEImpl button = new ButtonISWEImpl(text);
		return button;
	}
	
	static class PushButtonISWEImpl extends PushButton implements ISWEWidget{
		
		List listenerList = new ArrayList();
		Map metaInfo = new HashMap();
		
		public PushButtonISWEImpl(Image imgUp, Image imgDown) {
			super(imgUp, imgDown);
		}
		public PushButtonISWEImpl(Image imgUp) {
			super(imgUp);
		}

		public void addSetVisibleListener(Listener listener) {
			listenerList.add(listener);
		}
		
		public void setVisible(boolean visible) {
			for (int i = 0; i < listenerList.size(); i++) {
				((Listener)listenerList.get(i)).execute(new Boolean(visible));
			}
			super.setVisible(visible);
		}
		public Map getWidgetMetaInfo() {
			return metaInfo;
		}
	}
	
	static class ButtonISWEImpl extends Button implements ISWEWidget{
		
		List listenerList = new ArrayList();
		Map metaInfo = new HashMap();
		
		public ButtonISWEImpl(String text) {
			super(text);
		}

		public void addSetVisibleListener(Listener listener) {
			listenerList.add(listener);
		}
		
		public void setVisible(boolean visible) {
			for (int i = 0; i < listenerList.size(); i++) {
				((Listener)listenerList.get(i)).execute(new Boolean(visible));
			}
			super.setVisible(visible);
		}
		public Map getWidgetMetaInfo() {
			return metaInfo;
		}
	}
	
}
